package tech.mobi.child.utils;

import java.util.ArrayList;

import tech.mobi.child.bean.MainClientAppBean;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

	private static final int VERSION = 2;

	// private final String CREATE_TABLE_HOME =
	// "CREATE TABLE HOME(HOMEID INTEGER PRIMARY KEY AUTOINCREMENT,"
	// +"USERNAME VARCHAR(30),"
	// +"TITLE VARCHAR(30),"
	// +"ICON VARCHAR(30),"
	// +"CLASS VARCHAR(100))";

	private final String CREATE_TABLE_BASE = " CREATE TABLE [T_IMOBILE_APPS] ( "
			+ "[APPID] INTEGER PRIMARY KEY, "
			+ "[APPNAME] TEXT(20) DEFAULT (''), "
			+ "[APPPIC] TEXT(100) DEFAULT (''), "
			+ "[APPTYPE] TEXT(20) DEFAULT (''), "
			+ "[PARID] TEXT(20) DEFAULT (''), "
			+ "[DATABASEID] TEXT(20) DEFAULT (''), "
			+ "[APPDESC] TEXT(500) DEFAULT (''), "
			+ "[APPSORT] DECIMAL(10, 4) DEFAULT (0), "
			+ "[IS_MYAPP] TEXT(1) DEFAULT ('N')) ";

	private final String CREATE_TABLE_DIC1 = " CREATE TABLE [T_IMOBILE_APPS_TYPE_DIC] ( "
			+
			// "[INDEX] INTEGER PRIMARY KEY AUTOINCREMENT, "+
			"[ID] INTEGER PRIMARY KEY, "
			+ "[NAME] TEXT(200) DEFAULT (''), "
			+ "[SORT] DECIMAL(10, 4) DEFAULT (0), "
			+ "[IS_VALID] TEXT(1) DEFAULT ('Y')) ";

	// private final String INSERT_APPS1 =
	// "INSERT INTO T_IMOBILE_APPS "+
	// "(APPID,APPNAME,APPPIC,APPTYPE,PARID,DATABASEID,APPDESC,APPSORT,IS_MYAPP) "
	// +
	// "VALUES "+
	// "(1,'账单明细','s_billing','1','0','0','轻松管理积分账单',1,'N') ";

	private final String INSERT_APPS_TYPE1 = "INSERT INTO T_IMOBILE_APPS_TYPE_DIC "
			+ "(ID,NAME,SORT,IS_VALID) " + "VALUES " + "(1,'积分交易',1,'Y') ";
	private final String INSERT_APPS_TYPE2 = "INSERT INTO T_IMOBILE_APPS_TYPE_DIC "
			+ "(ID,NAME,SORT,IS_VALID) " + "VALUES " + "(2,'生活服务',2,'Y') ";
	private final String INSERT_APPS_TYPE3 = "INSERT INTO T_IMOBILE_APPS_TYPE_DIC "
			+ "(ID,NAME,SORT,IS_VALID) " + "VALUES " + "(3,'休闲娱乐',3,'Y') ";
	private final String INSERT_APPS_TYPE4 = "INSERT INTO T_IMOBILE_APPS_TYPE_DIC "
			+ "(ID,NAME,SORT,IS_VALID) " + "VALUES " + "(4,'其他',4,'Y') ";

	public DBOpenHelper(Context context) {
		super(context, "imobile.db", null, VERSION);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL(CREATE_TABLE_BASE);
		db.execSQL(CREATE_TABLE_DIC1);

		String strSQL = "";
		ArrayList<MainClientAppBean> list = new ArrayList<MainClientAppBean>();
		list = (ArrayList<MainClientAppBean>) DBAppSource.getAppList();
		for (int i = 0; i < list.size(); i++) {
			strSQL = "INSERT INTO T_IMOBILE_APPS "
					+ "(APPID,APPNAME,APPPIC,APPTYPE,PARID,DATABASEID,APPDESC,APPSORT,IS_MYAPP) "
					+ "VALUES " + "(" + list.get(i).getAppid() + ",'"
					+ list.get(i).getAppname() + "','"
					+ list.get(i).getApppic() + "','"
					+ list.get(i).getApptype() + "','" + list.get(i).getParid()
					+ "','" + list.get(i).getDatabaseid() + "','"
					+ list.get(i).getAppdesc() + "',"
					+ list.get(i).getAppsort() + ",'"
					+ list.get(i).getIs_myapp() + "') ";
			db.execSQL(strSQL);
		}
		
		db.execSQL(INSERT_APPS_TYPE1);
		db.execSQL(INSERT_APPS_TYPE2);
		db.execSQL(INSERT_APPS_TYPE3);
		db.execSQL(INSERT_APPS_TYPE4);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}
